package com.jerei.web.utils.spring;

import com.jerei.plugin.exceptions.PluginNotInstallException;
import com.jerei.web.plugin.api.IController;
import com.jerei.web.plugin.api.IPluginConfig;
import com.jerei.web.utils.UtilsPluginManager;
import com.jerei.web.utils.UtilsUI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import ro.fortsoft.pf4j.Plugin;
import ro.fortsoft.pf4j.PluginState;
import ro.fortsoft.pf4j.PluginWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by sunfeihu on 2016/11/23.
 */
@Controller
@RequestMapping(value = "/admin/plugin")
public class AdminPluginControl extends AdminBaseControl {

    @RequestMapping(value = "/{pluginId}/{op}", method = RequestMethod.GET, produces = "text/html")
    public Object get(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable String pluginId, @PathVariable String op) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, PluginNotInstallException {
        PluginWrapper pluginWrapper = UtilsPluginManager.get().getPlugin(pluginId);
        if (null == pluginWrapper || null == pluginWrapper.getPlugin()) {
            model.addAttribute("message", "插件未安装");
            return "/admin/main/plugin/notice";
        }
        Plugin plugin = pluginWrapper.getPlugin();
        if (plugin.getWrapper().getPluginState().equals(PluginState.STOPPED)) {
            model.addAttribute("message", "插件已禁用");
            return "/admin/main/plugin/notice";
        }
        if (plugin.getWrapper().getPluginState().equals(PluginState.DISABLED)) {
            model.addAttribute("message", "插件已禁用");
            return "/admin/main/plugin/notice";
        }
        model.addAttribute("pluginPath", "/admin/plugins" + pluginWrapper.getPluginPath() + "/classes");
        //如果操作是config，判断是否拥有配置项，如果有，显示配置
        if (op.equals("pluginConfig")) {
            IPluginConfig iPluginConfig = UtilsPluginManager.getExtension(IPluginConfig.class, pluginId);
            return iPluginConfig.pluginConfig(request, response, model, plugin);
        }
        List<IController> controllers = UtilsPluginManager.getExtensions(IController.class, pluginId);
        if (null != controllers && controllers.size() > 0) {
            for (IController controller : controllers) {
                if (controller.haveMethod(op)) {
                    return controller.get(request, response, model, op, plugin);
                }
            }
        }
        return "/admin/plugins/" + plugin.getWrapper().getPluginPath() + "/classes/" + op;
    }

    @RequestMapping(value = "/{pluginId}/{op}", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Object post(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable String pluginId, @PathVariable String op) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, PluginNotInstallException {
        PluginWrapper pluginWrapper = UtilsPluginManager.get().getPlugin(pluginId);
        if (null == pluginWrapper || null == pluginWrapper.getPlugin()) {
            return UtilsUI.tip("插件未安装");
        }
        Plugin plugin = pluginWrapper.getPlugin();
        if (plugin.getWrapper().getPluginState().equals(PluginState.STOPPED)) {
            return UtilsUI.tip("插件已禁用");
        }
        if (plugin.getWrapper().getPluginState().equals(PluginState.DISABLED)) {
            return UtilsUI.tip("插件已禁用");
        }
        model.addAttribute("pluginPath", "/admin/plugins" + pluginWrapper.getPluginPath() + "/classes");
        //如果操作是config，判断是否拥有配置项，如果有，显示配置
        if (op.equals("savePluginConfig")) {
            IPluginConfig iPluginConfig = UtilsPluginManager.getExtension(IPluginConfig.class, pluginId);
            return iPluginConfig.savePluginConfig(request, response, model, plugin);
        }
        List<IController> controllers = UtilsPluginManager.getExtensions(IController.class, pluginId);
        if (null != controllers && controllers.size() > 0) {
            for (IController controller : controllers) {
                if (controller.haveMethod(op)) {
                    return controller.get(request, response, model, op, plugin);
                }
            }
        }
        return UtilsUI.tip("方法未实现");
    }

}